<?php

class BkController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
        	$this->_redirect("/bk-login/");
        }
        
		//Make menu
		$mod_menu = new Db_Tables_Menu();
		$this->view->main_menu = $mod_menu->MenuDecolation($_SESSION['identity']['user_type']);
		
		$this->view->layout = "backend";
    }
	
	function indexAction()
	{
		
	}
	
	function profileAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->title = "编辑个人信息";
		
		if(1 == $params['act'])
		{
			$this->view->msg = '更新成功。';
		}elseif(2 == $params['act'])
		{
			$this->view->msg = '更新失败。';
		}
	}
	
	function profileSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['pwd'] && $params['pwd'] == $params['pwd2'])
		{
			$mod_admin = new Db_Tables_AdminUsers();
			$row = $mod_admin->fetchRow("admin_id='".$_SESSION['identity']['id']."'");
			$row->admin_pwd = sha1($params['pwd']);
			$row->save();
			
			$this->_redirect('/bk/profile/act/1');
		}else{
			$this->_redirect('/bk/profile/act/2');
		}
	}
	
	function adminAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "管理员列表";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "邮箱已存在，添加失败。";
		}
		
		$mod_users_joins = new Db_Joins_UsersJoins();
		
		if($params['keyword'])
		{
			$mod_users_joins->keyword = $params['keyword'];
		}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetAdminList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function editAdminAction()
	{
		$params = $this->_request->getParams();
		
		if(1 == $params['status'])
		{
			$this->view->msg = "更新成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "两次输入的密码不一致，更新失败";
		}
    	
    	if($params['id'])
    	{
    		$this->view->admin_id = $params['id'];
    		
    		$mod_users_joins = new Db_Joins_UsersJoins();
			$mod_users_joins->admin_id = $params['id'];
			$this->view->admin_info = $mod_users_joins->GetAdminInfo();

			$this->view->title = "编辑管理员 - ".$this->view->admin_info['admin_name'];
    	}else{
    		echo "关键参数丢失";
			die;
    	}
		
		$mod_groups = new Db_Tables_AdminGroups();
		$this->view->groups = $mod_groups->MakeCategoryArray();
	}
	
	function editAdminSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['admin_id'])
		{
			$mod_admin = new Db_Tables_AdminUsers();
			$row = $mod_admin->fetchRow("admin_email='".$params['admin_email']."' and admin_id !='".$params['admin_id']."'");
			
			if(!$row)
			{
				if($params['admin_pwd'] == $params['admin_pwd_confirm'])
				{
					$row = $mod_admin->fetchRow("admin_id='".$params['admin_id']."'");

					if($row)
					{
						$row->admin_email = $params['admin_email'];
						$row->admin_group_id = $params['admin_group_id'];
						$row->admin_name = $params['admin_name'];
						if($params['admin_pwd'])
						{
							$row->admin_pwd = sha1($params['admin_pwd']);
						}
						$row->admin_status = $params['admin_status'];
						if($row->save())
						{
							$this->_redirect("/bk/edit-admin/id/".$params['admin_id']."/status/1");
						}
					}
				}else{
					$this->_redirect("/bk/edit-admin/id/".$params['admin_id']."/status/2"); //password incorrect
				}
			}else{
				$this->_redirect("/bk/edit-admin/id/".$params['admin_id']."/status/3"); //admin exists
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function addAdminAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "添加管理员";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}
		
		$mod_groups = new Db_Tables_AdminGroups();
		$this->view->groups = $mod_groups->MakeCategoryArray();
	}
	
	function addAdminSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['admin_email'] && $params['admin_name'])
		{
			$mod_admin = new Db_Tables_AdminUsers();
			$row = $mod_admin->fetchRow("admin_email='".$params['admin_email']."'");
			
			if(!$row)
			{
				$row = $mod_admin->createRow();
				$row->admin_email = $params['admin_email'];
				$row->admin_pwd = sha1(123456);
				$row->admin_group_id = $params['admin_group_id'];
				$row->admin_status = 1;
				$row->admin_name = $params['admin_name'];
				$admin_id = $row->save();

				if($admin_id)
				{
					$this->_redirect("/bk/admin/status/1");
				}
			}else{
				$this->_redirect("/bk/admin/status/2");
			}
		}
	}
	
	function newsAction()
	{
		$params = $this->_request->getParams();
		
		if(1 == $params['status'])
		{
			$this->view->msg = "更新成功";
		}
    	
    	$this->view->title = "页顶公告";
		
		$mod_params = new Db_Tables_SysParams();
		$this->view->info = $mod_params->BaseInfo();
	}
	
	function newsSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['news_switch'])
		{
			$news_switch = 1;
		}else{
			$news_switch = 0;
		}
		
		$mod_param = new Db_Tables_SysParams();
		
		$row = $mod_param->fetchRow("pkey = 'news_switch'");
		$row->pval = $news_switch;
		$row->save();
		
		$row = $mod_param->fetchRow("pkey = 'news_contents'");
		$row->pval = $params['news_contents'];
		$row->save();
		
		$this->_redirect("/bk/news/status/1");
	}
	
	function homepageBannersAction()
	{
		$params = $this->_request->getParams();
		
		if(1 == $params['status'])
		{
			$this->view->msg = "更新成功";
		}
		
		if($params['channel_id'])
		{
			$channel_id = $params['channel_id'];
		}else{
			$channel_id = 0;
		}
    	
    	$this->view->title = "首页轮播图";
		
		$mod_params = new Db_Tables_SysParams();
		$this->view->info = $mod_params->BaseInfo();
		
		$mod_channels = new Db_Tables_Channels();
		$mod_channels->skip_province = 1;
		$this->view->channels = $mod_channels->GetChannels();
		
		$mod_banners = new Db_Tables_TopBanners();
		$mod_banners->load_all = 1;
		$mod_banners->channel_id = $channel_id;
		$this->view->banners = $mod_banners->LoadBanners();
		
		$this->view->channel_id = $channel_id;
		
		if($params['success'])
    	{
    		$this->view->msg = "保存成功";
    	}elseif(1 == $params['error']){
    		$this->view->msg = "出错：图片上传出错";
    	}
	}
	
	function homepageBannersSubmitAction()
	{
		$params = $this->_request->getParams();
//		$ext = new Sys_Extensions();
//		$ext->FormatArray($_FILES);
		$n = 0;
		
		$mod_banners = new Db_Tables_TopBanners();
		
    	foreach($params as $pkey => $pval)
		{
			$exp = explode("_", $pkey);
			
			if('url' == $exp[0])
			{
				$row = $mod_banners->fetchRow("tb_id=".$exp[1]);
				
				if($row)
				{
					//upload image
					if($_FILES['image_'.$exp[1]]['name'])
					{
						$image = "l0".$exp[1].time().$n;
						$n += 1;
						
						if('image/jpeg' == $_FILES['image_'.$exp[1]]['type'])
						{
							$file_name = $image.".jpg";
						}elseif('image/png' == $_FILES['image_'.$exp[1]]['type'])
						{
							$file_name = $image.".png";
						}else{
							$file_name = NULL;
							$error = 1;
						}
						
						$file_folder = "images/banners/";
						
						if($file_name)
						{
							if(move_uploaded_file($_FILES['image_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
							{
								$row->image_url = $file_name;
								$row->target_url = $params['url_'.$exp[1]];
								$row->ctime = date("Y-m-d H:i:s");
								$row->save();
							}else{
								$error = 1;
							}
						}
					}else{
						$row->target_url = $params['url_'.$exp[1]];
						$row->ctime = date("Y-m-d H:i:s");
						$row->save();
					}
				}
			}
		}
		
		if($error)
		{
			$this->_redirect("/bk/homepage-banners/channel_id/".$params['channel_id']."/error/".$error);
		}else{
			$this->_redirect("/bk/homepage-banners/channel_id/".$params['channel_id']."/success/1");
		}
    				
	}
	
	function homepageBannersDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'])
		{
			$mod_banners = new Db_Tables_TopBanners();
			$row = $mod_banners->fetchRow("tb_id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->target_url = NULL;
				$row->ctime = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/homepage-banners");
	}
	
	function level1Action()
	{
		$params = $this->_request->getParams();
		$this->view->title = "商场管理";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "添加失败。";
		}
		
		$mod_channels = new Db_Tables_Channels();
		$this->view->channels = $mod_channels->GetChannels();
		
		$mod_users_joins = new Db_Joins_ItemJoins();
		
		if($params['keyword'])
		{
			$mod_users_joins->keyword = $params['keyword'];
			$this->view->keyword = $params['keyword'];
		}
		
		if($params['channel_id'])
		{
			$mod_users_joins->channel_id = $params['channel_id'];
			$this->view->channel_id = $params['channel_id'];
		}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetLevel1ItemList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function level1EditAction()
	{
		$params = $this->_request->getParams();
		
		if(1 == $params['success'])
		{
			$this->view->msg = "更新成功";
		}elseif(1 == $params['error'])
		{
			$this->view->msg = "未找到数据";
		}elseif(2 == $params['error'])
		{
			$this->view->msg = "数据保存失败";
		}
    	
    	if($params['id'])
    	{
    		$this->view->id = $params['id'];
    		
    		$mod_users_joins = new Db_Joins_ItemJoins();
			$mod_users_joins->level_id = $params['id'];
			$this->view->info = $mod_users_joins->GetLevel1Info();
			
			$mod_channels = new Db_Tables_Channels();
			$mod_channels->skip_province = 1;
			$this->view->channels = $mod_channels->GetChannels();

			$this->view->title = "编辑商场 - ".$this->view->info['title'];

			$mod_banners = new Db_Tables_TopBanners();
			$mod_banners->load_all = 1;
			$mod_banners->level = 1;
			$mod_banners->level_id = $params['id'];
			$this->view->banners = $mod_banners->LoadBanners();
    	}else{
    		echo "关键参数丢失";
			die;
    	}
	}
	
	function level1EditSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['id'])
		{
			$mod_level1 = new Db_Tables_Level1();
			$row = $mod_level1->fetchRow("id=".$params['id']);
			
			if($row)
			{
				if($_FILES['image'])
				{
					$image = "l1".$params['id'].time()."0";
					
					if('image/jpeg' == $_FILES['image']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['image']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
						$error = 1;
					}
					
					$file_folder = "images/banners/";
					
					if($file_name)
					{
						move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
						$row->image_url = $file_name;
					}
				}
					
				$row->channel_id = $params['channel_id'];
				$row->title = $params['title'];
				$row->memo1 = $params['memo1'];
				$row->joined_members = $params['joined_members'];
				$row->status = $params['status'];
				if($row->save())
				{
					//上传banner
					$mod_banners = new Db_Tables_TopBanners();
					
					$n = 0;
					
					foreach($params as $pkey => $pval)
					{
						$exp = explode("_", $pkey);

						if('eurl' == $exp[0])
						{
							$row = $mod_banners->fetchRow("tb_id=".$exp[1]);

							if($row)
							{
								//upload image
								if($_FILES['ebanner_'.$exp[1]]['name'])
								{
									$image = "l1".$exp[1].time().$n;
									$n += 1;

									if('image/jpeg' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".jpg";
									}elseif('image/png' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".png";
									}else{
										$file_name = NULL;
										$error = 1;
									}

									$file_folder = "images/banners/";

									if($file_name)
									{
										if(move_uploaded_file($_FILES['ebanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
										{
											$row->image_url = $file_name;
											$row->target_url = $params['eurl_'.$exp[1]];
											$row->ctime = date("Y-m-d H:i:s");
											$row->save();
										}
									}
								}else{
									$row->target_url = $params['eurl_'.$exp[1]];
									$row->ctime = date("Y-m-d H:i:s");
									$row->save();
								}
							}
						}

						if('nurl' == $exp[0])
						{
							//upload image
							if($_FILES['nbanner_'.$exp[1]]['name'])
							{
								$image = "l1n".time().$n;
								$n += 1;
	
								if('image/jpeg' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".jpg";
								}elseif('image/png' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".png";
								}else{
									$file_name = NULL;
									$error = 1;
								}

								$file_folder = "images/banners/";

								if($file_name)
								{
									if(move_uploaded_file($_FILES['nbanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
									{
										$row = $mod_banners->createRow();
										$row->level = 1;
										$row->level_id = $params['id'];
										$row->image_url = $file_name;
										$row->target_url = $params['nurl_'.$exp[1]];
										$row->ctime = date("Y-m-d H:i:s");
										$row->save();
									}
								}
							}
						}
					}
					
					$this->_redirect("/bk/level1-edit/id/".$params['id']."/success/1"); //成功
				}else{
					$this->_redirect("/bk/level1-edit/id/".$params['id']."/error/2"); //数据保存失败
				}
				
			}else{
				$this->_redirect("/bk/level1-edit/id/".$params['id']."/error/1"); //未找到数据
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function level1RefreshAction()
	{
		$params = $this->_request->getParams();
    	
    	if($params['id'])
    	{
    		$mod = new Db_Tables_Level1();
			$row = $mod->fetchRow("id='".$params['id']."'");
			
			if($row)
			{
				$row->ctime = date("Y-m-d H:i:s");
				$row->save();
			}
    	}
		
		$this->_redirect("/bk/level1");
	}
	
	function level2RefreshAction()
	{
		$params = $this->_request->getParams();
    	
    	if($params['id'])
    	{
    		$mod = new Db_Tables_Level2();
			$row = $mod->fetchRow("id='".$params['id']."'");
			
			if($row)
			{
				$row->ctime = date("Y-m-d H:i:s");
				$row->save();
			}
    	}
		
		$this->_redirect("/bk/level2/id/".$params['level1_id']);
	}
	
	function level3RefreshAction()
	{
		$params = $this->_request->getParams();
    	
    	if($params['id'])
    	{
    		$mod = new Db_Tables_Level3();
			$row = $mod->fetchRow("id='".$params['id']."'");
			
			if($row)
			{
				$row->ctime = date("Y-m-d H:i:s");
				$row->save();
			}
    	}
		
		$this->_redirect("/bk/level3/id/".$params['level2_id']);
	}
	
	function level1AddAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->title = "添加商场";
		
		$mod_channels = new Db_Tables_Channels();
		$mod_channels->skip_province = 1;
		$this->view->channels = $mod_channels->GetChannels();
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}
	}
	
	function level1AddSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['title'])
		{
			$mod_level1 = new Db_Tables_Level1();
			$row = $mod_level1->createRow();
			
			if($_FILES['image'])
			{
				$image = "l1n".time()."0";
				
				if('image/jpeg' == $_FILES['image']['type'])
				{
					$file_name = $image.".jpg";
				}elseif('image/png' == $_FILES['image']['type'])
				{
					$file_name = $image.".png";
				}else{
					$file_name = NULL;
					$error = 1;
				}
				
				$file_folder = "images/banners/";
				
				if($file_name)
				{
					move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
					$row->image_url = $file_name;
				}
			}
					
			$row->channel_id = $params['channel_id'];
			$row->title = $params['title'];
			$row->memo1 = $params['memo1'];
			$row->joined_members = $params['joined_members'];
			$row->status = $params['status'];
			$row->ctime = date("Y-m-d H:i:s");
			$row->admin_id = $_SESSION['identity']['id'];
			if($row->save())
			{
				$this->_redirect("/bk/level1/status/1"); //成功
			}else{
				$this->_redirect("/bk/level1/status/2"); //数据保存失败
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function level1ImageDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'])
		{
			$mod = new Db_Tables_Level1;
			$row = $mod->fetchRow("id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level1-edit/id/".$params['id']);
	}
	
	function level2ImageDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'] && $params['level1_id'])
		{
			$mod = new Db_Tables_Level2();
			$row = $mod->fetchRow("id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level2-edit/level1_id/".$params['level1_id']."/id/".$params['id']);
	}
	
	function level3ImageDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'] && $params['level2_id'])
		{
			$mod = new Db_Tables_Level3();
			$row = $mod->fetchRow("id = ".$params['id']);
			
			if($row)
			{
				$row->banner_url = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level3-edit/level2_id/".$params['level2_id']."/id/".$params['id']);
	}
	
	function level2Action()
	{
		$params = $this->_request->getParams();
		$this->view->title = "商户管理";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "添加失败。";
		}
		
		$mod_users_joins = new Db_Joins_ItemJoins();
		
		$mod_users_joins->level_id = $params['id'];
		
		$this->view->level1_id = $params['id'];
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetLevel2ItemList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function level1BannerDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'])
		{
			$mod_banners = new Db_Tables_TopBanners();
			$row = $mod_banners->fetchRow("tb_id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->target_url = NULL;
				$row->ctime = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level1-edit/id/".$row->level_id);
	}
	
	function level2BannerDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'] && $params['level1_id'])
		{
			$mod_banners = new Db_Tables_TopBanners();
			$row = $mod_banners->fetchRow("tb_id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->target_url = NULL;
				$row->ctime = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level2-edit/level1_id/".$params['level1_id']."/id/".$row->level_id);
	}
	
	function level3BannerDelAction()
	{
		$params = $this->_request->getParams();
		
		if($params['id'] && $params['level2_id'])
		{
			$mod_banners = new Db_Tables_TopBanners();
			$row = $mod_banners->fetchRow("tb_id = ".$params['id']);
			
			if($row)
			{
				$row->image_url = NULL;
				$row->target_url = NULL;
				$row->ctime = NULL;
				$row->save();
			}
		}
		
		$this->_redirect("/bk/level3-edit/level2_id/".$params['level2_id']."/id/".$row->level_id);
	}
	
	function level2AddAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->level1_id = $params['level1_id'];
		
		$this->view->title = "添加商户";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}
	}
	
	function level2AddSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['level1_id'])
		{
			$mod_level2 = new Db_Tables_Level2();
			$row = $mod_level2->createRow();
			
			if($_FILES['image'])
			{
				$image = "l2n".time()."0";
				
				if('image/jpeg' == $_FILES['image']['type'])
				{
					$file_name = $image.".jpg";
				}elseif('image/png' == $_FILES['image']['type'])
				{
					$file_name = $image.".png";
				}else{
					$file_name = NULL;
					$error = 1;
				}
				
				$file_folder = "images/banners/";
				
				if($file_name)
				{
					move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
					$row->image_url = $file_name;
				}
			}
					
			$row->level1_id = $params['level1_id'];
			$row->contents = $params['contents'];
			$row->joined_members = $params['joined_members'];
			$row->status = $params['status'];
			$row->ctime = date("Y-m-d H:i:s");
			$row->admin_id = $_SESSION['identity']['id'];
			if($row->save())
			{
				$this->_redirect("/bk/level2/id/".$params['level1_id']."/status/1"); //成功
			}else{
				$this->_redirect("/bk/level2/".$params['level1_id']."/status/2"); //数据保存失败
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function level2EditAction()
	{
		$params = $this->_request->getParams();
		
		if(1 == $params['success'])
		{
			$this->view->msg = "更新成功";
		}elseif(1 == $params['error'])
		{
			$this->view->msg = "未找到数据";
		}elseif(2 == $params['error'])
		{
			$this->view->msg = "数据保存失败";
		}
		
		$this->view->level1_id = $params['level1_id'];
    	
    	if($params['id'])
    	{
    		$this->view->id = $params['id'];
    		
    		$mod_users_joins = new Db_Joins_ItemJoins();
			$mod_users_joins->level_id = $params['id'];
			$this->view->info = $mod_users_joins->GetLevel2Info();

			$this->view->title = "编辑商户";

			$mod_banners = new Db_Tables_TopBanners();
			$mod_banners->load_all = 1;
			$mod_banners->level = 2;
			$mod_banners->level_id = $params['id'];
			$this->view->banners = $mod_banners->LoadBanners();
    	}else{
    		echo "关键参数丢失";
			die;
    	}
	}
	
	function level2EditSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['id'])
		{
			$mod_level2 = new Db_Tables_Level2();
			$row = $mod_level2->fetchRow("id=".$params['id']);
			
			if($row)
			{
				if($_FILES['image'])
				{
					$image = "l2".$params['id'].time()."0";
					
					if('image/jpeg' == $_FILES['image']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['image']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
						$error = 1;
					}
					
					$file_folder = "images/banners/";
					
					if($file_name)
					{
						move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
						$row->image_url = $file_name;
					}
				}
				
				$row->level1_id = $params['level1_id'];
				$row->contents = $params['contents'];
				$row->joined_members = $params['joined_members'];
				$row->status = $params['status'];
				if($row->save())
				{
					//上传banner
					$mod_banners = new Db_Tables_TopBanners();
					
					$n = 0;
					
					foreach($params as $pkey => $pval)
					{
						$exp = explode("_", $pkey);

						if('eurl' == $exp[0])
						{
							$row = $mod_banners->fetchRow("tb_id=".$exp[1]);

							if($row)
							{
								//upload image
								if($_FILES['ebanner_'.$exp[1]]['name'])
								{
									$image = "l2".$exp[1].time().$n;
									$n += 1;

									if('image/jpeg' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".jpg";
									}elseif('image/png' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".png";
									}else{
										$file_name = NULL;
										$error = 1;
									}

									$file_folder = "images/banners/";

									if($file_name)
									{
										if(move_uploaded_file($_FILES['ebanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
										{
											$row->image_url = $file_name;
											$row->target_url = $params['eurl_'.$exp[1]];
											$row->ctime = date("Y-m-d H:i:s");
											$row->save();
										}
									}
								}else{
									$row->target_url = $params['eurl_'.$exp[1]];
									$row->ctime = date("Y-m-d H:i:s");
									$row->save();
								}
							}
						}

						if('nurl' == $exp[0])
						{
							//upload image
							if($_FILES['nbanner_'.$exp[1]]['name'])
							{
								$image = "l2n".time().$n;
								$n += 1;
	
								if('image/jpeg' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".jpg";
								}elseif('image/png' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".png";
								}else{
									$file_name = NULL;
									$error = 1;
								}

								$file_folder = "images/banners/";

								if($file_name)
								{
									if(move_uploaded_file($_FILES['nbanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
									{
										$row = $mod_banners->createRow();
										$row->level = 2;
										$row->level_id = $params['id'];
										$row->image_url = $file_name;
										$row->target_url = $params['nurl_'.$exp[1]];
										$row->ctime = date("Y-m-d H:i:s");
										$row->save();
									}
								}
							}
						}
					}
					
					$this->_redirect("/bk/level2-edit/level1_id/".$params['level1_id']."/id/".$params['id']."/success/1"); //成功
				}else{
					$this->_redirect("/bk/level2-edit/level1_id/".$params['level1_id']."/id/".$params['id']."/error/2"); //数据保存失败
				}
				
			}else{
				$this->_redirect("/bk/level2-edit/level1_id/".$params['level1_id']."/id/".$params['id']."/error/1"); //未找到数据
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function level3Action()
	{
		$params = $this->_request->getParams();
		$this->view->title = "活动管理";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "添加失败。";
		}
		
		$mod_users_joins = new Db_Joins_ItemJoins();
		
		$mod_users_joins->level_id = $params['id'];
		
		$this->view->level2_id = $params['id'];
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetLevel3ItemList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function level3AddAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->level2_id = $params['level2_id'];
		
		$this->view->call_editor = 1;
		
		$this->view->title = "添加活动";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}
	}
	
	function level3AddSubmitAction()
	{
		$params = $this->_request->getParams();
//		var_dump($params);die;
		if($params['level2_id'])
		{
			$mod_level3 = new Db_Tables_Level3();
			$row = $mod_level3->createRow();
			
			if($_FILES['image'])
			{
				$image = "l3n".time()."0";
				
				if('image/jpeg' == $_FILES['image']['type'])
				{
					$file_name = $image.".jpg";
				}elseif('image/png' == $_FILES['image']['type'])
				{
					$file_name = $image.".png";
				}else{
					$file_name = NULL;
					$error = 1;
				}
				
				$file_folder = "images/banners/";
				
				if($file_name)
				{
					move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
					$row->banner_url = $file_name;
				}
			}
			
			$mod_level2 = new Db_Tables_Level2();
			$level2_row = $mod_level2->fetchRow("id=".$params['level2_id']);
					
			$row->level1_id = $level2_row['level1_id'];
			$row->level2_id = $params['level2_id'];
			$row->joined_members = $params['joined_members'];
			$row->category_id = $params['category_id'];
			$row->banner_text = $params['banner_text'];
			$row->slogan = $params['slogan'];
			$row->contents = $params['editorValue'];
			
			if($params['external_url'])
			{
				$row->external_url = trim($params['external_url']);
				
				$exp1 = explode("&", trim($params['external_url']));
				
				if(!empty($exp1))
				{
					foreach($exp1 as $exp1_val)
					{
						$exp2 = explode("=", $exp1_val);
						
						if('marketId' == $exp2[0])
						{
							$row->lcb_market_id = $exp2[1];
							break;
						}
					}
				}
			}
			
			$row->status = $params['status'];
			$row->ctime = date("Y-m-d H:i:s");
			$row->admin_id = $_SESSION['identity']['id'];
			if($row->save())
			{
				$this->_redirect("/bk/level3/id/".$params['level2_id']."/status/1"); //成功
			}else{
				$this->_redirect("/bk/level3/id/".$params['level2_id']."/status/2"); //数据保存失败
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function level3EditAction()
	{
		$params = $this->_request->getParams();
		$this->view->call_editor = 1;
		
		if(1 == $params['success'])
		{
			$this->view->msg = "更新成功";
		}elseif(1 == $params['error'])
		{
			$this->view->msg = "未找到数据";
		}elseif(2 == $params['error'])
		{
			$this->view->msg = "数据保存失败";
		}
		
		$this->view->level2_id = $params['level2_id'];
    	
    	if($params['id'])
    	{
    		$this->view->id = $params['id'];
    		
    		$mod_users_joins = new Db_Joins_ItemJoins();
			$mod_users_joins->level_id = $params['id'];
			$this->view->info = $mod_users_joins->GetLevel3Info();

			$this->view->title = "编辑活动";

			$mod_banners = new Db_Tables_TopBanners();
			$mod_banners->load_all = 1;
			$mod_banners->level = 3;
			$mod_banners->level_id = $params['id'];
			$this->view->banners = $mod_banners->LoadBanners();
    	}else{
    		echo "关键参数丢失";
			die;
    	}
	}
	
	function level3EditSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['id'])
		{
			$mod_level3 = new Db_Tables_Level3();
			$row = $mod_level3->fetchRow("id=".$params['id']);
			
			if($row)
			{
				if($_FILES['image'])
				{
					$image = "l3".$params['id'].time()."0";
					
					if('image/jpeg' == $_FILES['image']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['image']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
						$error = 1;
					}
					
					$file_folder = "images/banners/";
					
					if($file_name)
					{
						move_uploaded_file($_FILES['image']['tmp_name'], $file_folder.$file_name);
						$row->banner_url = $file_name;
					}
				}
				
				$row->joined_members = $params['joined_members'];
				$row->category_id = $params['category_id'];
				$row->banner_text = $params['banner_text'];
				$row->slogan = $params['slogan'];
				$row->contents = $params['editorValue'];
				
				if($params['external_url'])
				{
					$row->external_url = trim($params['external_url']);

					$exp1 = explode("&", trim($params['external_url']));

					if(!empty($exp1))
					{
						foreach($exp1 as $exp1_val)
						{
							$exp2 = explode("=", $exp1_val);

							if('marketId' == $exp2[0])
							{
								$row->lcb_market_id = $exp2[1];
								break;
							}
						}
					}
				}else{
					$row->external_url = NULL;
					$row->lcb_market_id = NULL;
				}

				$row->status = $params['status'];
				$row->ctime = date("Y-m-d H:i:s");
				$row->admin_id = $_SESSION['identity']['id'];
				if($row->save())
				{
					//上传banner
					$mod_banners = new Db_Tables_TopBanners();
					
					$n = 0;
					
					foreach($params as $pkey => $pval)
					{
						$exp = explode("_", $pkey);

						if('eurl' == $exp[0])
						{
							$row = $mod_banners->fetchRow("tb_id=".$exp[1]);

							if($row)
							{
								//upload image
								if($_FILES['ebanner_'.$exp[1]]['name'])
								{
									$image = "l3".$exp[1].time().$n;
									$n += 1;

									if('image/jpeg' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".jpg";
									}elseif('image/png' == $_FILES['ebanner_'.$exp[1]]['type'])
									{
										$file_name = $image.".png";
									}else{
										$file_name = NULL;
										$error = 1;
									}

									$file_folder = "images/banners/";

									if($file_name)
									{
										if(move_uploaded_file($_FILES['ebanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
										{
											$row->image_url = $file_name;
											$row->target_url = $params['eurl_'.$exp[1]];
											$row->ctime = date("Y-m-d H:i:s");
											$row->save();
										}
									}
								}else{
									$row->target_url = $params['eurl_'.$exp[1]];
									$row->ctime = date("Y-m-d H:i:s");
									$row->save();
								}
							}
						}

						if('nurl' == $exp[0])
						{
							//upload image
							if($_FILES['nbanner_'.$exp[1]]['name'])
							{
								$image = "l3n".time().$n;
								$n += 1;
	
								if('image/jpeg' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".jpg";
								}elseif('image/png' == $_FILES['nbanner_'.$exp[1]]['type'])
								{
									$file_name = $image.".png";
								}else{
									$file_name = NULL;
									$error = 1;
								}

								$file_folder = "images/banners/";

								if($file_name)
								{
									if(move_uploaded_file($_FILES['nbanner_'.$exp[1]]['tmp_name'], $file_folder.$file_name))
									{
										$row = $mod_banners->createRow();
										$row->level = 3;
										$row->level_id = $params['id'];
										$row->image_url = $file_name;
										$row->target_url = $params['nurl_'.$exp[1]];
										$row->ctime = date("Y-m-d H:i:s");
										$row->save();
									}
								}
							}
						}
					}
					
					$this->_redirect("/bk/level3-edit/level2_id/".$params['level2_id']."/id/".$params['id']."/success/1"); //成功
				}else{
					$this->_redirect("/bk/level3-edit/level2_id/".$params['level2_id']."/id/".$params['id']."/error/2"); //数据保存失败
				}
				
			}else{
				$this->_redirect("/bk/level3-edit/level2_id/".$params['level2_id']."/id/".$params['id']."/error/1"); //未找到数据
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function membersAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "会员管理";
		
		$mod_users_joins = new Db_Joins_UsersJoins();
		
		if($params['keyword'])
		{
			$mod_users_joins->keyword = $params['keyword'];
		}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetMemberList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function agentsAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "商户列表";
		
		$mod_users_joins = new Db_Joins_UsersJoins();
		
		if($params['keyword'])
		{
			$mod_users_joins->keyword = $params['keyword'];
		}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_users_joins->page_id = $page_id;
    	}
		
		$result = $mod_users_joins->GetAgentList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function topicsAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "文章列表";
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "添加失败";
		}
		
		$mod_joins = new Db_Joins_TopicsJoins();
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_joins->page_id = $page_id;
    	}
		
		$result = $mod_joins->GetTopicList();
		
		$this->view->data = $result['current_page_info'];

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}
		
		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		$this->view->current_page_id = $page_id;
		$this->view->params_string = $params_string;
	}
	
	function editTopicAction()
	{
		$params = $this->_request->getParams();
		$this->view->call_editor = 1;
		
		if(1 == $params['status'])
		{
			$this->view->msg = "更新成功";
		}elseif(2 == $params['status'])
		{
			$this->view->msg = "更新失败";
		}
    	
    	if($params['id'])
    	{
    		$this->view->topic_id = $params['id'];
    		
    		$mod_joins = new Db_Joins_TopicsJoins();
			$mod_joins->topic_id = $params['id'];
			$this->view->topic_info = $mod_joins->GetTopicInfo();

			$this->view->title = "编辑文章 - ".$this->view->topic_info['title'];
    	}else{
    		echo "关键参数丢失";
			die;
    	}
	}
	
	function editTopicSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['topic_id'])
		{
			$mod_topics = new Db_Tables_Topics();
			$row = $mod_topics->fetchRow("topic_id='".$params['topic_id']."'");
			
			if($row)
			{
				$row->title= $params['title'];
				$row->contents= $params['editorValue'];
				if($row->save())
				{
					$this->_redirect("/bk/edit-topic/id/".$params['topic_id']."/status/1");
				}else{
					$this->_redirect("/bk/edit-topic/id/".$params['topic_id']."/status/2");
				}
			}
		}else{
			echo "关键参数丢失";
			die;
		}
	}
	
	function addTopicAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "添加文章";
		$this->view->call_editor = 1;
		
		if(1 == $params['status'])
		{
			$this->view->msg = "添加成功";
		}
	}
	
	function addTopicSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$a = new Sys_Extensions();
//		$a->FormatArray($params);
//		die;
		
		if($params['title'])
		{
			$mod_topics = new Db_Tables_Topics();
			
			$row = $mod_topics->createRow();
			$row->title = $params['title'];
			$row->contents = $params['editorValue'];
			$row->ctime = date("Y-m-d H:i:s");
			$row->admin_id = $_SESSION['identity']['id'];
			$topic_id = $row->save();
			
			if($topic_id)
			{
				$this->_redirect("/bk/topics/status/1");
			}else{
				$this->_redirect("/bk/topics/status/2");
			}
		}
	}
    
    function qrCodeAction()
    {
		include ('phpqrcode/qrlib.php');
		$tempDir = "images/banners/";
		$params = $this->_request->getParams();
		
		if($params['level'] && $params['level_id'])
		{
			$mod_params = new Db_Tables_SysParams();
			$base_url = $mod_params->GetVal('base_url');
			
			if(1 == $params['level'])
			{
				$codeContents = $base_url.'index/level1/level_id/'.$params['level_id'];
			}elseif(2 == $params['level'])
			{
				$codeContents = $base_url.'index/level2/level_id/'.$params['level_id'];
			}elseif(3 == $params['level'])
			{
				$codeContents = $base_url.'index/level3/level_id/'.$params['level_id'];
			}
			
			// generating
			QRcode::png($codeContents, $tempDir.'qr_1.png', QR_ECLEVEL_L, 2);
			QRcode::png($codeContents, $tempDir.'qr_2.png', QR_ECLEVEL_L, 6);
			QRcode::png($codeContents, $tempDir.'qr_3.png', QR_ECLEVEL_L, 10);

			// displaying
			echo '<img src="/'.$tempDir.'qr_1.png" />';
			echo '<img src="/'.$tempDir.'qr_2.png" />';
			echo '<img src="/'.$tempDir.'qr_3.png" />';
		}else{
			echo "关键参数丢失";
			die;
		}
		
    	die;
    }
}